package cn.iocoder.yudao.module.member.controller.admin.jobcollection;

import cn.iocoder.yudao.module.member.dal.dataobject.job.ApiJobDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.service.job.ApiJobService;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.jobcollection.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.jobcollection.JobCollectionDO;
import cn.iocoder.yudao.module.member.service.jobcollection.JobCollectionService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 我的收藏")
@RestController
@RequestMapping("/member/job-collection")
@Validated
public class JobCollectionController {

    @Resource
    private JobCollectionService jobCollectionService;

    @Resource
    private MemberUserService memberUserService;

    @Resource
    private ApiJobService apiJobService;

    @PostMapping("/create")
    @Operation(summary = "创建我的收藏")
    public CommonResult<Long> createJobCollection(@Valid @RequestBody JobCollectionSaveReqVO createReqVO) {
        return success(jobCollectionService.createJobCollection(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新我的收藏")
    public CommonResult<Boolean> updateJobCollection(@Valid @RequestBody JobCollectionSaveReqVO updateReqVO) {
        jobCollectionService.updateJobCollection(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除我的收藏")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteJobCollection(@RequestParam("id") Long id) {
        jobCollectionService.deleteJobCollection(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得我的收藏")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<JobCollectionRespVO> getJobCollection(@RequestParam("id") Long id) {
        JobCollectionDO jobCollection = jobCollectionService.getJobCollection(id);
        return success(BeanUtils.toBean(jobCollection, JobCollectionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得我的收藏分页")
    public CommonResult<PageResult<JobCollectionRespVO>> getJobCollectionPage(@Valid JobCollectionPageReqVO pageReqVO) {
        PageResult<JobCollectionDO> pageResult = jobCollectionService.getJobCollectionPage(pageReqVO);
        PageResult<JobCollectionRespVO> bean = BeanUtils.toBean(pageResult, JobCollectionRespVO.class);
        for (JobCollectionRespVO respVO:bean.getList()){
            ApiJobDO job = apiJobService.getJobs(respVO.getJobId());
            if (Objects.nonNull(job)) {
                respVO.setJobName(job.getJobName());
            }
            MemberUserDO user = memberUserService.getUser(respVO.getUserId());
            if (Objects.nonNull(user)) {
                respVO.setUserName(user.getNickname());
            }
        }
        return success(bean);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出我的收藏 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportJobCollectionExcel(@Valid JobCollectionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JobCollectionDO> list = jobCollectionService.getJobCollectionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "我的收藏.xls", "数据", JobCollectionRespVO.class,
                        BeanUtils.toBean(list, JobCollectionRespVO.class));
    }

}